package com.example.clublesson01.fragments.search

import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.bw.kf.lib_base.base.BaseActivity
import com.bw.kf.lib_base.base.BaseFragment
import com.example.clublesson01.R
import com.example.clublesson01.adapter.PlayCartInfoAdapter
import com.example.clublesson01.adapter.SearchTitleAdapter
import com.example.clublesson01.databinding.ActivitySearchBinding
import com.example.clublesson01.fragments.playcart.PlayCartViewModel

@Route(path = "/app/search/SearchActivity")
class SearchActivity : BaseActivity<ActivitySearchBinding,PlayCartViewModel>() {
    private lateinit var searchTitleAdapter: SearchTitleAdapter
    var playCartInfoAdapter = PlayCartInfoAdapter()
    override fun iniView() {
        searchTitleAdapter = SearchTitleAdapter()
        binding.titlerv.adapter = searchTitleAdapter
        viewModel.getSearchTitle()
        searchTitleAdapter.setOnItemClickListener { adapter, view, position ->
            binding.playCartSearch.setText(searchTitleAdapter.data[position].topicName)
        }
        binding.search.setOnClickListener {
            playCartInfoAdapter=PlayCartInfoAdapter()

            viewModel.getPalyCartInfoListsearch("0",binding.playCartSearch.text.toString(), 1, 10)
            // 设置播放车信息列表的适配器
            binding.searchrv.adapter = playCartInfoAdapter
            // 设置播放车信息列表的布局管理器为线性布局管理器
            binding.searchrv.layoutManager =LinearLayoutManager(this)
        }




    }

    override fun iniData() {
        viewModel.playCartInfoSuccess.observe(this){
            if (it.code == 200){
                playCartInfoAdapter.setNewData(it.data.records)
                Log.i("searchrecords", "iniData: "+it.data.records)
            }
        }
        viewModel.playCartInfoFail.observe(this){
            Log.e("SearchError", "请求失败: $it")
        }
        viewModel.searchtitlesuccess.observe(this){
            if (it.code == 200){
                val topicList = it.data.topicList
                searchTitleAdapter.setNewData(topicList.toMutableList())
            }
        }
        viewModel.searchtitlefail.observe(this) { error ->
            Log.e("SearchError", "请求失败: $error")
        }
    }

    override fun getViewModel(): Class<PlayCartViewModel> {
        return PlayCartViewModel::class.java
    }

    override fun Layoutid()=R.layout.activity_search

}